package cn.mado.org.service.impl;

import cn.mado.basic.util.PageList;
import cn.mado.org.domain.User;
import cn.mado.org.dto.LoginDto;
import cn.mado.org.mapper.UserMapper;
import cn.mado.org.query.UserQuery;
import cn.mado.org.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public void add(User user) {
        user.setCreationTime(new Date());
        userMapper.add(user);
    }

    @Override
    public void delete(Long userId) {
        userMapper.delete(userId);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public User findById(Long userId) {
        return userMapper.findById(userId);
    }

    @Override
    public PageList<User> queryByPage(UserQuery query) {
        Integer totals = userMapper.queryByCount(query);
        List<User> users = userMapper.queryByPage(query);
        return new PageList<>(totals,users);
    }

    @Override
    public User login(LoginDto loginDto) {
        System.out.println("service:"+loginDto);
        User user = userMapper.login(loginDto.getUsername(),loginDto.getPassword());
        System.out.println("user:"+user);
        if (user == null){
            throw new RuntimeException("账号或密码错误！！！");
        }
        if (user.getState() != 1){
            throw new RuntimeException("当前账号未启用！！");
        }
        return user;
    }
}
